package Test2;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class TestXml {

    public static void main(String[] args) throws DocumentException {
//        获取解析器对象
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src\\xml\\student.xml"));
        Element rootElement = document.getRootElement();
        List<Element> list = rootElement.elements("student");

        ArrayList<Student> arrayList = new ArrayList<Student>();


        for (Element element : list) {
            Attribute id = element.attribute("id");
            String text = id.getText();

            Element name = element.element("name");
            String text1 = name.getText();

            Element age = element.element("age");
            String text2 = age.getText();



            Student s = new Student(text,text1,Integer.parseInt(text2));
            arrayList.add(s);
        }

        for (Student student : arrayList) {
            System.out.println(student);
        }


    }
}

class Student{
    private String id;
    private String name;
    private int age;

    public Student() {
    }

    public Student(String id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}